package com.aaa.backsystem.controller;

import com.aaa.backsystem.entity.*;
import com.aaa.backsystem.service.PetrolStationService;
import com.aaa.backsystem.service.impl.FileUploadService;
import com.aaa.backsystem.util.MyConstants;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author gyc
 * @since 2020-07-23
 */
@Controller
@RequestMapping("/petrolStation")
public class PetrolStationController extends BaseController{

    @Autowired
    private PetrolStationService petrolStationService;

    @Autowired
    private FileUploadService fileUploadService;

    /**
     * 跳转加油站基本信息地址
     *
     * @return
     */
    @RequestMapping("/toShowPetrolStation")
    public String toShowPetrolStation() {
        return "petrolstationinfo/info";
    }

    /**
     * 显示派出所名称
     *
     * @return
     */
    @RequestMapping("/selectDept")
    @ResponseBody
    public List<Dept> selectDept() {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        if (user.getPetrolStationId() != null) {
            List<PetrolStation> psList = petrolStationService.selectByPetrolStationId(user.getPetrolStationId());
            List<Dept> deptList = petrolStationService.selectDept(psList.get(0).getDeptId());
            return deptList;
        }
        return null;
    }

    /**
     * 油站基本信息表格
     *
     * @return
     */
    @RequestMapping("/selectPetrolStation")
    @ResponseBody
    public List<PetrolStation> selectPetrolStation() {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        List<PetrolStation> psList = petrolStationService.selectByPetrolStationId(user.getPetrolStationId());
        return psList;
    }

    /**
     * 文件上传到oss
     */
    @RequestMapping("/uploadPic")
    @ResponseBody
    public FileUploadResult upload(@RequestParam("file") MultipartFile uploadFile) throws Exception {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        FileUploadResult result = new FileUploadResult();
        if(user.getPetrolStationId()!=null){
            result = fileUploadService.upload(uploadFile);
            String url = "https://" + result.getName();
            PetrolStation ps = new PetrolStation();
            ps.setPetrolStationId(user.getPetrolStationId());
            ps.setPic(url);
            petrolStationService.updateById(ps);
            return result;
        }else{
            result.setResponse(MyConstants.OPERATION_FAIL_MESSAGE);
            return  result;
        }
    }
}

